from HTMLTestRunner import HTMLTestRunner
import unittest
import time
import os

# 准备用例的路径
url = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
# case用例路径
case_url = url + '\\case'
print(case_url)
# 存放测试报告路径
report_url = url + '/report'

# loader
loader = unittest.defaultTestLoader.discover(case_url, '*.py')

# runner
# 准备写入文件路径
# report = os.path.abspath(report_url)

# 准备文件，文件名规则：年月日时分秒-login.html
filename = time.strftime('%Y-%m-%d-%H-%M-%S') + '-ecshop.html'

# 拼接文件路径
filepath = os.path.join(report_url, filename)

# 写入文件，执行测试用例，生成测试报告
with open(filepath, 'wb') as f:
    runner = HTMLTestRunner(
        title='综合测试报告',
        stream=f,
        verbosity=2
     )

    # 执行
    runner.run(loader)